package com.zjpavt.client.extenddevice.networkdevice;

import com.zjpavt.client.enums.ExtendDeviceEnum;
import com.zjpavt.client.hal.ExtendDeviceManager;
import com.zjpavt.client.hal.network.NetworkManager;
import com.zjpavt.client.udp.UdpSender;
import com.zjpavt.client.util.LoggerUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SunLiteDevice {
    public static final int DESTINATION_PORT = 2430;

    private static String getBroadcast() {
        return NetworkManager.getInstance().getBroadcastAddress(NetworkManager.INTERFACE_ETH_0);
    }

    /**
     * 发送suite场景。
     * 从文件中获取suite设备的IP地址 如果获取不到则使用默认的IP=192.168.1.220
     * 可以编辑suite_device_ip,指定UDP地址 第四位地址可以设置到255发UDP广播。
     * @param sceneId
     * @return
     */
    public static boolean sendSuiteScene(int sceneId) {
        long startTime = System.currentTimeMillis();
        log.info("sendSuiteScene SCENE_ID =" + sceneId);
        String s = "53697564695f37426d000" + sceneId + "000000010000000000";
        byte[] bytes = new byte[s.length() / 2];
        for (int i = 0; i < s.length() / 2; i++) {
            bytes[i] = (byte) (Character.digit(s.charAt(2 * i), 16) * 16
                               + Character.digit(s.charAt(2 * i + 1), 16));
        }
        UdpSender.sendUdpPacket(getBroadcast(), DESTINATION_PORT, bytes);
        log.info("DESTINATION_IP = " + getBroadcast() + " SCENE_ID= " + sceneId+ " msg=" + s + " use time=" +(System.currentTimeMillis() -startTime));
//		为了加速 优先发送udp指令 然后切换suite信号源
        try {
            if (ExtendDeviceManager.getInstance().containsExtendDevice(ExtendDeviceEnum.DMX_EXCHANGE)) {
//                JnIMethod.changeDmxInputMouth('B');
            }
        } catch (Exception e) {
            log.error(LoggerUtil.convertException2String(e));
        }
        return true;
    }
}
